package model.dao.phieutiemchung;

import java.util.Date;
import java.util.List;
import model.dao.AbstractQuery;
import model.dao.ProjectQuery;
import model.dao.hosotiemchung.HoSoTiemChungDAO;
import model.entity.HoSoTiemChung;
import model.entity.PhieuTiemChung;
import model.entity.Thuoc;
import model.entity.Vaccine;
import model.initialize.Initialize;
import model.util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author luanp_000
 */
public class PhieuTiemChungDAO {
    public static Vaccine timVaccineTheoMa(int maVaccine){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
        List<Vaccine> list = session.createQuery("FROM Vaccine AS vaccine WHERE vaccine.vaccineMa = ?").setInteger(0, maVaccine).list();
        session.getTransaction().commit();
        if(list.size() >0){
            return list.get(0);
        }
        return null;
    }
    public static Vaccine timVaccineTheoTen(String tenVaccine){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
        List<Vaccine> list = session.createQuery("FROM Vaccine AS vaccine WHERE vaccine.vaccineTen = ?").setString(0, tenVaccine).list();
        session.getTransaction().commit();
        if(list.size() >0){
            return list.get(0);
        }
        return null;
    }
    public static boolean themPhieuTiemChung(int maHoSoTiemChung, String tenVaccine, String lieuTiem, Date ngayTiem, Date ngayTiemKe){
        HoSoTiemChung hoSoTiemChung = HoSoTiemChungDAO.timTheoMaHoSo_HoSo(maHoSoTiemChung);
        Vaccine vaccine = timVaccineTheoTen(tenVaccine);
        if(hoSoTiemChung != null){
            if(vaccine != null){
                Session session = HibernateUtil.getSessionFactory().getCurrentSession();
                session.getTransaction().begin();
                PhieuTiemChung phieuTiemChung = new PhieuTiemChung(hoSoTiemChung, vaccine, lieuTiem, ngayTiem, ngayTiemKe);
                session.save(phieuTiemChung);
                session.getTransaction().commit();
                return true;
            }
        }
        return false;
    }
    public static boolean xoaPhieuTiemChung(int maPhieuTiemChung){
        PhieuTiemChung phieuTiemChung = timPTCTheoMa(maPhieuTiemChung);
        if(phieuTiemChung != null){
                Session session = HibernateUtil.getSessionFactory().getCurrentSession();
                session.getTransaction().begin();
                session.delete(phieuTiemChung);
                session.getTransaction().commit();
                return true;
        }
        return false;
    }
    public static boolean xoaPTCTheoMaHoSo(int maHoSoTiemChung){
        List<PhieuTiemChung> list = timPTCTheoMaHoSo(maHoSoTiemChung);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        for (PhieuTiemChung phieuTiemChung : list) {
            session.delete(phieuTiemChung);
        }
        session.getTransaction().commit();
        return true;
    }
    public static PhieuTiemChung timPTCTheoMa(int maPhieuTiemChung){
        Session sesion = HibernateUtil.getSessionFactory().getCurrentSession();
        sesion.getTransaction().begin();
        List<PhieuTiemChung> list = sesion.createQuery("FROM PhieuTiemChung AS phieuTiemChung WHERE phieuTiemChung.ptcMa = ?").setInteger(0, maPhieuTiemChung).list();
        for (PhieuTiemChung phieuTiemChung : list) {
            Initialize.initializePhieuTiemChung(phieuTiemChung);
        }
        sesion.getTransaction().commit();
        if(list.size() > 0){
            return list.get(0);
        }
        return null;
    }
    public static List<PhieuTiemChung> timPTCTheoMaHoSo(int maHoSoTiemChung){
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        List<PhieuTiemChung> list = session.createQuery("FROM PhieuTiemChung AS  phieuTiemChung WHERE phieuTiemChung.hoSoTiemChung.hstcMa = ?")
                .setInteger(0, maHoSoTiemChung)
                .list();
        for (PhieuTiemChung phieuTiemChung : list) {
            Initialize.initializePhieuTiemChung(phieuTiemChung);
        }
        session.getTransaction().commit();
        return list;
    }
    /**
     * Load tất cả phiếu tiêm chủng vào 1 List
     *
     * @return List danh sách tất cả phiếu tiêm chủng. Mỗi 1 đối tượng trong
     * List trả về là 1 dòng (row) trong database.
     */
    public static final List loaddAllPTC() {
        return AbstractQuery.loadObject(ProjectQuery.LOAD_ALL_PTC);
    }

    public static final Thuoc load1Thuoc(int ptcMa) {
        List list = AbstractQuery.loadObject(ProjectQuery.LOAD_1_PTC + ptcMa);
        return (Thuoc) list.get(0);
    }

    /**
     * Thêm mới 1 phiếu tiêm chủng
     *
     * @param ptc đối tượng phiếu tiêm chủng cần thêm
     */
    public final static void insertPTC(PhieuTiemChung ptc) {
        AbstractQuery.insertObject(ptc);
    }

    /**
     * Sửa thông tin 1 phiếu tiêm chủng.
     *
     * @param ptc đối tượng phiếu tiêm chủng sẽ được sửa thông tin.
     */
    public static final void updatePTC(PhieuTiemChung ptc) {
        AbstractQuery.updateObject(ptc);
    }

    /**
     * Xóa 1 phiếu tiêm chủng.
     *
     * @param ptcMa mã đối phiếu tiêm chủng sẽ bị xóa.
     */
    public static final void deletePTC(int ptcMa) {
        AbstractQuery.modifyObject(3, (Thuoc) AbstractQuery.loadObject(ProjectQuery.LOAD_1_PTC + ptcMa).get(0));
    }
}
